package com.spring.ioc.demo.test;

import com.spring.ioc.demo.domain.User;
import com.spring.ioc.demo.factory.support.DefaultListableBeanFactory;
import com.spring.ioc.demo.reader.XmlBeanDefinitionReader;
import com.spring.ioc.demo.resource.ClasspathResource;
import com.spring.ioc.demo.resource.Resource;
import com.spring.ioc.demo.service.impl.UserServiceImpl;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用面向对象思维和配置文件的方式去实现容器化管理Bean
 */
public class SpringIocTest03 {

    private DefaultListableBeanFactory beanFactory;

    // 注册BeanDefinition流程
    @Before
    public void loadXml(){
        //完成XML解析，其实就是完成BeanDefinition的注册
        // XML解析，解析的结果，放入beanDefinitions中
        String location = "beansDemo.xml";

        // 获取流对象(使用了策略模式)
        Resource resource = new ClasspathResource(location);
        InputStream inputStream = resource.getResource();

        // 按照spring定义的标签语义去解析Document文档
        beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        beanDefinitionReader.loadBeanDefinitions(inputStream);
    }

    @Test
    public void Test(){
        UserServiceImpl userService = (UserServiceImpl) beanFactory.getBean("userService");

        //实现用户查询功能
        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");

        List<User> users = userService.queryUsers(map);
        System.out.println(users);
    }
}
